package com.ideal.service.access.impl;

import com.ideal.service.access.PropertiesAccessService;
import com.ideal.service.cache.PropertiesCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @Author songfz
 * @Description
 * @Date 2022/8/8
 */
@Service
public class PropertiesAccessServiceImpl implements PropertiesAccessService {

    @Autowired
    private PropertiesCacheService propertiesCacheService;

    /**
     * 获取value值
     * @param fileEnName 缓存文件key
     * @param key 查询的key值
     * @return
     */
    @Override
    public String getProperties(String fileEnName, String key){
        final Map map = propertiesCacheService.getContents(fileEnName);
        return String.valueOf(map.get(key));
    }


    /**
     * 获取value集合
     * @param fileEnName 缓存文件key
     * @param key 查询的key值
     * @return
     */
    @Override
    public List getPropertiesList(String fileEnName, String key){

        final List<String> result = new ArrayList<>();

        final String value = getProperties(fileEnName, key);
        final StringTokenizer tokenizer = new StringTokenizer(value, ",");

        while (tokenizer.hasMoreTokens()) {
            result.add(tokenizer.nextToken());
        }
        return result;
    }

    /**
     *
     * @param fileEnName
     * @param context
     * @return
     */
    @Override
    public boolean addPropertiesCache(String fileEnName, Map<String, String> context){
        return propertiesCacheService.addPropertiesCache(fileEnName,context);
    }

    @Override
    public boolean fuzzyDelKeyAll(String key) {
        return propertiesCacheService.fuzzyDelKeyAll(key);
    }
}
